package com.dingwen.treasure.base.exception;


import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.base.pojo.vo.ResultGenerator;


/**
 * 业务异常
 *
 * @author dingwen
 * @date 2021.06.29 10:18
 */
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 返回结果
     */
    private Result result;

    public Result result() {
        return result;
    }

    /**
     * 服务异常
     *
     * @param result 后果
     */
    public BusinessException(Result result) {
        super(result.getMessage());
        this.result = result;
    }

    /**
     * 服务异常
     *
     * @param result    后果
     * @param throwable 丢弃
     */
    public BusinessException(Result result, Throwable throwable) {
        super(result.getMessage(), throwable);
        this.result = result;
    }

    /**
     * 服务异常
     *
     * @param message 消息
     */
    public BusinessException(String message) {
        result = ResultGenerator.genFailureResult(message);
    }

    /**
     * 服务异常
     *
     * @param throwable 原因
     */
    public BusinessException(Throwable throwable) {
        super(throwable);
    }

    /**
     * 服务异常
     *
     * @param message   消息
     * @param throwable 原因
     */
    public BusinessException(String message, Throwable throwable) {
        super(message, throwable);
        result = ResultGenerator.genFailureResult(message);
    }

    /**
     * 收到信息
     *
     * @return {@link String}
     */
    @Override
    public String getMessage() {
        return this.result.toString();
    }

}
